﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using Dapper.Extensions;
using Ymatou.ServiceCenter.Domain.IRepositories;
using Ymatou.ServiceCenter.Domain.Models;
using Ymatou.CommonService;

namespace Ymatou.ServiceCenter.Repository
{
    public class ServiceConfigRepository : RepositoryBase<ServiceConfig>, IServiceConfigRepository
    {
        public ServiceConfig GetServiceConfig(string consumerAppId, string providerAppId)
        {
            return base.GetById("Default", "serviceconfig", new { ConsumerAppId = consumerAppId, ProviderAppId = providerAppId });
        }

        public IList<ServiceConfig> GetServiceConfigByConsumerAppId(string consumerAppId)
        {
            if (string.IsNullOrWhiteSpace(consumerAppId))
                throw new ArgumentNullException("consumerAppId");
            DynamicParameters dynamicParameters = new DynamicParameters();
            dynamicParameters.Add("ConsumerAppId", consumerAppId);
            return base.GetList("Default", "serviceconfig", "ConsumerAppId=@ConsumerAppId", dynamicParameters);
        }

        public IList<ServiceConfig> GetServiceConfigByProviderAppId(string providerAppId)
        {
            if (string.IsNullOrWhiteSpace(providerAppId))
                throw new ArgumentNullException("providerAppId");
            DynamicParameters dynamicParameters = new DynamicParameters();
            dynamicParameters.Add("ProviderAppId", providerAppId);
            return base.GetList("Default", "serviceconfig", "ProviderAppId=@ProviderAppId", dynamicParameters);
        }

        public IList<ServiceConfig> GetServiceConfigs(string appId, long? lastUpdateTicks)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("ConsumerAppId=@ConsumerAppId");
            DynamicParameters dynamicParameters = new DynamicParameters();
            dynamicParameters.Add("ConsumerAppId", appId, 50);

            if (lastUpdateTicks != null)
            {
                sql.Append(" AND LastUpdateTicks>@LastUpdateTicks");
                dynamicParameters.Add("LastUpdateTicks", lastUpdateTicks);
            }
            return base.GetList("Default", "serviceconfig", sql.ToString(), dynamicParameters);
        }

        public int TryInsert(ServiceConfig entity)
        {
            try
            {
                this.Insert("Default", "serviceconfig", entity);
                return 1;
            }
            catch (Exception ex)
            {
                ApplicationLog.Error(string.Format("[TryInsertServiceConfig]异常：{0}", ex.Message), ex);
                return -1;
            }
        }

        public bool TryUpdate(ServiceConfig entity)
        {
            try
            {
                bool updateResult = this.Update("Default", "serviceconfig", entity);
                return updateResult;
            }
            catch (Exception ex)
            {
                ApplicationLog.Error(string.Format("[TryUpdateServiceConfig]异常：{0}", ex.Message), ex);
                return false;
            }
        }
    }
}
